package com.yang.dao.impl;

import com.yang.dao.MyOrderDao;
import com.yang.entity.RelationEntity;
import com.yang.pojo.MyOrderDto;
import com.yang.util.PublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Repository
public class MyorderDaoImpl implements MyOrderDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    public List<MyOrderDto> selectMyorder(long userId, String liveStatus, String guestStatus) {
//        String sql = "SELECT guest.name,room.room_num,order_form.begin_day,order_form.stay_day,order_form.rantal,order_form.pay" +
//                " FROM guest,room,order_form,liver_relation,user WHERE user.id = ? AND user.id = liver_relation.user_id " +
//                "AND liver_relation.status = ? AND liver_relation.guest_id = guest.id AND guest.status = ? " +
//                "AND liver_relation.order_form_id = order_form.id AND liver_relation.room_id = room.id;";
//        ResultSetExtractor<List<MyOrderDto>> lists = new ResultSetExtractor<List<MyOrderDto>>() {
//            @Override
//            public List<MyOrderDto> extractData(ResultSet rs) throws SQLException, DataAccessException {
//                List<MyOrderDto> lists = new ArrayList<MyOrderDto>();
//                while(rs.next()) {
//                    MyOrderDto myOrderDto = new MyOrderDto();
//                    myOrderDto.setName(rs.getString("name"));
//                    myOrderDto.setRoomNum(rs.getString("room_num"));
//                    myOrderDto.setBeginTime(PublicUtil.chargeMillisToDay(rs.getLong("begin_day")));
//                    myOrderDto.setStayDay(Integer.parseInt(rs.getString("stay_day")));
//                    myOrderDto.setRetal(rs.getFloat("rantal"));
//                    myOrderDto.setPay(rs.getFloat("pay"));
//                    lists.add(myOrderDto);
//                }
//                return lists;
//            }
//        };
//        return jdbcTemplate.query(sql,new Object[] {userId,liveStatus,guestStatus},lists);
        List<MyOrderDto> myOrderDtos = new ArrayList<MyOrderDto>();
        String hql = "FROM RelationEntity WHERE userId.id = ? AND status = ? AND  guestId.status =?";
        List<RelationEntity> relationEntities = (List<RelationEntity>) hibernateTemplate.find(hql,new Object[]{userId,liveStatus,guestStatus});
        if(relationEntities.isEmpty()){
            return myOrderDtos;
        }
         for(RelationEntity relationEntity : relationEntities){
            MyOrderDto myOrderDto = new MyOrderDto();
            myOrderDto.setName(relationEntity.getGuestId().getName());
            myOrderDto.setRoomNum(relationEntity.getRoomId().getRoomNum());
            myOrderDto.setBeginTime(PublicUtil.chargeMillisToDay(relationEntity.getOrderFormId().getBeginDay()));
            myOrderDto.setStayDay(relationEntity.getOrderFormId().getStayDay());
            myOrderDto.setRetal(relationEntity.getOrderFormId().getRantal());
            myOrderDto.setPay(relationEntity.getOrderFormId().getPay());
            myOrderDtos.add(myOrderDto);
        }
         return myOrderDtos;


    }
}
